package com.turing.usdtpay.http;

import android.content.Context;
import android.text.TextUtils;


import com.turing.usdtpay.common.SPKeyConstants;
import com.turing.usdtpay.entity.LoginData;
import com.turing.usdtpay.http.callback.ApiCallback;
import com.turing.usdtpay.http.okhttp.callback.OkCallback;
import com.turing.usdtpay.http.okhttp.parser.OkTextParser;
import com.turing.usdtpay.manager.UserManager;
import com.turing.usdtpay.service.PushService;
import com.turing.usdtpay.utils.EncryptUtils;
import com.turing.usdtpay.utils.LogUtils;
import com.turing.usdtpay.utils.SPHelper;
import com.turing.usdtpay.utils.SortUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by $Jay
 * Date : 2019\1\15 0015
 **/
public class ApiWrapper {


    public static void printDebugMsg(String url, HashMap<String, String> headers, HashMap<String, String> params, String... args) {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder builder = new StringBuilder();
        if (!TextUtils.isEmpty(url)) {
            stringBuilder.append("url -> " + url + "\n");
            builder.append(url+"&");
        }

        if (headers != null && headers.size() > 0) {
            stringBuilder.append("header ==>> \n");
            Iterator<Map.Entry<String, String>> iterator = headers.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                stringBuilder.append(entry.getKey() + "=" + entry.getValue() + "\n");

            }
        }

        if (params != null && params.size() > 0) {
            stringBuilder.append("param ==>> \n");
            Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                stringBuilder.append(entry.getKey() + "=" + entry.getValue() + "\n");
                builder.append(entry.getKey() + "=" + entry.getValue() + "&");

            }
        }

        if (args != null && args.length > 0) {
            for (int i = 0; i < args.length - 2; i++) {
                stringBuilder.append(args[i] + "\n");
            }
            stringBuilder.append(args[args.length - 1]);
        }

        LogUtils.d(ApiService.REQ_TAG + stringBuilder.toString());
        LogUtils.d(ApiService.REQ_TAG + builder.toString());
    }



    public static OkCallback wrapJsonOkCallBack(final ApiCallback callback){
        OkCallback<String> okCallback = new OkCallback<String>(new OkTextParser()) {
            @Override
            public void onSuccess(int code, String response) {
                callback.onSuccessFilter(code,response);
            }

            @Override
            public void onFailure(Throwable e) {
                callback.onFailureFilter(e);
            }
        };
        return okCallback;
    }

    public static OkCallback wrapArrayOkCallBack(final ApiCallback callback){
        OkCallback<String> okCallback = new OkCallback<String>(new OkTextParser()) {
            @Override
            public void onSuccess(int code, String response) {
                callback.onSuccessArrayFilter(code,response);
            }

            @Override
            public void onFailure(Throwable e) {
                callback.onFailureFilter(e);
            }
        };
        return okCallback;
    }



    public static HashMap<String,String> wrapApiParams(HashMap<String,String> params){
        HashMap<String, String> apiParams = new HashMap<>();
        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (entry.getValue() == null) {
                    apiParams.put(entry.getKey(), "null");
                } else {
                    apiParams.put(entry.getKey(), entry.getValue());
                }
            }
        }
        //参数value，统一做去空格处理
        for(Map.Entry<String, String> entry : apiParams.entrySet()){
            entry.setValue(entry.getValue().trim());
        }
        return apiParams;
    }


    /**
     * 有公共参数
     * @param params
     * @return
     */
    public static HashMap<String,String> wrapApiParamsWithCommon(Context context,HashMap<String, String> params){
        HashMap<String, String> apiParams = new HashMap<>();
        addCommonParam(context,apiParams,params);
        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (entry.getValue() == null) {
                    apiParams.put(entry.getKey(), "null");
                } else {
                    apiParams.put(entry.getKey(), entry.getValue());
                }
            }
        }
        //参数value，统一做去空格处理
        for(Map.Entry<String, String> entry : apiParams.entrySet()){
            entry.setValue(entry.getValue().trim());
        }
        return apiParams;
    }
    /**
     * 添加公共的参数
     * @param newMap  新的Map
     */
    private static void addCommonParam(Context context,HashMap<String, String> newMap, HashMap<String,String> oldMap) {
        SPHelper helper = new SPHelper(context,SPKeyConstants.TR_SDK_CONFIG);
        String uid = helper.getString(SPKeyConstants.PARAM_KEY_UID);
        String verify = helper.getString(SPKeyConstants.PARAM_KEY_VERIFY);
//        LogUtils.d("uid :"+uid+", verify :"+verify);
        oldMap.put(SPKeyConstants.PARAM_KEY_TRADER,uid);
        oldMap.put(SPKeyConstants.PARAM_KEY_VERIFY,verify);
        SortUtils utils = new SortUtils();
        String sign = utils.sortParams(oldMap);
        String md5 = EncryptUtils.encrypt32BitMD5(sign);
//        LogUtils.d("sort result MD5-->" + md5);
        newMap.put(SPKeyConstants.PARAM_KEY_SIGN, md5);
    }
}
